
package org.javaqq.face.main.gui;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.*;

import org.javaqq.face.util.BufferedImageConverter;
import org.javaqq.face.util.ImageUtils;

/**
 * A custom component, used to show images in a frame.
 * 
 * @author Yana Stamcheva
 */
public class FramedImage
    extends JComponent
{
    private final Image frameImage;

    private BufferedImage icon;

    public BufferedImage getIcon() {
		return icon;
	}

	public void setIcon(BufferedImage iconImage) {
		if(this.icon!=null){
			this.icon=null;
		}
		this.icon = getHeadImage(
				iconImage, width-2, height-2);;
		if (this.isVisible())
        {
            this.revalidate();
            this.repaint();
        }
	}

	private final int width;

    private final int height;

    /**
     * Creates a FramedImage by specifying the width and the height of the
     * label. These are used to paint the image frame in the correct bounds.
     * 
     * @param imageIcon the icon to show within the frame
     * @param width the width of the frame
     * @param height the height of the frame
     */
    public FramedImage(Image frame,BufferedImage image, int width, int height)
    {
        this.width = width;
        this.height = height;

        this.setPreferredSize(new Dimension(width, height));

        this.frameImage
            = ImageUtils
                .scaleImageWithinBounds(
                		frame,
                    width,
                    height);

        
        if (image != null){
            this.icon = getHeadImage(
            		image, width, height);
        }
    }
    
    public static BufferedImage getHeadImage(BufferedImage image,int width,int height){
		return BufferedImageConverter.getFasterScaledInstance(image, width, height, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
	}

    /**
     * Creates a FramedImage by specifying the width and the height of the frame.
     * 
     * @param width the width of the frame
     * @param height the height of the frame
     */
    public FramedImage(int width, int height)
    {
        this(null,null, width, height);
    }

    /**
     * Sets the image to display in the frame.
     * 
     * @param image the image to display in the frame
     */
    public void setImageIcon(byte[] image)
    {
        try {
			icon = getHeadImage(BufferedImageConverter.createByteImage(image), width - 2, height - 2);
		} catch (IOException e) {
			e.printStackTrace();
		}

        if (this.isVisible())
        {
            this.revalidate();
            this.repaint();
        }
    }

    /**
     * Paints the contained image in a frame.
     *
     * Overrides {@link JComponent#paintComponent(Graphics)}.
     */
    public void paintComponent(Graphics g)
    {
        

        int frameWidth = frameImage.getWidth(this);
        int frameHeight = frameImage.getHeight(this);
        if ((frameWidth != -1) && (frameHeight != -1))
            g.drawImage(
                frameImage,
                width / 2 - frameWidth / 2,
                height / 2 - frameHeight / 2,
                null);
        
        if (icon != null)
        {
            int imageWidth = icon.getWidth();
            int imageHeight = icon.getHeight();
            if ((imageWidth != -1) && (imageHeight != -1))
                g.drawImage(
                		icon,
                    width / 2 - imageWidth / 2,
                    height / 2 - imageHeight / 2,
                    null);
        }
    }
}
